﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WarFrota.Models;

namespace WarFrota.Controllers
{ 
    public class ManutencaoController : Controller
    {
        private WarFrotaContext db = new WarFrotaContext();

        //
        // GET: /Manutencao/

        public ViewResult Index()
        {
            return View(db.Manutencoes.ToList());
        }

        //
        // GET: /Manutencao/Details/5

        public ViewResult Details(int id)
        {
            Manutencao manutencao = db.Manutencoes.Find(id);
            return View(manutencao);
        }

        //
        // GET: /Manutencao/Create

        public ActionResult Create()
        {
            ViewData["tipos"] = db.TiposManutencao.ToList();
            return View();
        } 

        //
        // POST: /Manutencao/Create

        [HttpPost]
        public ActionResult Create(Manutencao manutencao)
        {
            if (ModelState.IsValid)
            {
                TipoManutencao tipo = db.TiposManutencao.Find(manutencao.TipoManutencaoId);
                manutencao.Custo = tipo.CustoUnitario;
                db.Manutencoes.Add(manutencao);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(manutencao);
        }
        
        //
        // GET: /Manutencao/Edit/5
 
        public ActionResult Edit(int id)
        {
            Manutencao manutencao = db.Manutencoes.Find(id);
            ViewData["tipos"] = db.TiposManutencao.ToList();
            return View(manutencao);
        }

        //
        // POST: /Manutencao/Edit/5

        [HttpPost]
        public ActionResult Edit(Manutencao manutencao)
        {
            if (ModelState.IsValid)
            {
                db.Entry(manutencao).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(manutencao);
        }

        //
        // GET: /Manutencao/Delete/5
 
        public ActionResult Delete(int id)
        {
            Manutencao manutencao = db.Manutencoes.Find(id);
            return View(manutencao);
        }

        //
        // POST: /Manutencao/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Manutencao manutencao = db.Manutencoes.Find(id);
            db.Manutencoes.Remove(manutencao);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}